/*
 * @Author: your name
 * @Date: 2021-04-05 15:30:51
 * @LastEditTime: 2021-04-20 16:10:10
 * @LastEditors: Please set LastEditors
 * @Description: In User Settings Edit
 * @FilePath: \chronic-back-end\module\consultMod\controller.go
 */
package consultMod

import (
	"chronic/module/userMod"
	"net/http"
	"strconv"

	"github.com/gin-gonic/gin"
)

type consultationDto struct {
	DoctorID string `form:"doctorId" json:"doctorId" binding:"required"`
	Content  string `form:"content" json:"content" binding:"required"`
}

func consult(ctx *gin.Context) {
	user := ctx.MustGet("user").(userMod.User)
	var input consultationDto
	if err := ctx.ShouldBind(&input); err != nil {
		ctx.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
		return
	}
	doctorid, _ := strconv.ParseUint(input.DoctorID, 10, 64)
	con, err := addOneConsultation(user.ID, uint(doctorid), input.Content)
	if err != nil {
		ctx.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
	}
	ctx.JSON(http.StatusOK, con)
}

func getHistory(ctx *gin.Context) {
	user := ctx.MustGet("user").(userMod.User)
	cons, err := findConsultationsByPatientId(user.ID)
	if err != nil {
		ctx.JSON(http.StatusNotFound, gin.H{"error": err.Error()})
	}
	ctx.JSON(http.StatusOK, cons)
}

func getAllConsult(ctx *gin.Context) {
	// user := ctx.MustGet("user").(userMod.User)
	cons, err := findAllConsultations()
	if err != nil {
		ctx.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
	} else {
		ctx.JSON(http.StatusOK, cons)
	}
}

func getconsultsByUserId(ctx *gin.Context) {
	id := ctx.Query("userId")
	userId, _ := strconv.ParseUint(id, 10, 64)
	consults, err := findConsultsByUserId(uint(userId))
	if err != nil {
		ctx.JSON(http.StatusNotFound, "未找到！！")
	} else {
		ctx.JSON(http.StatusOK, consults)
	}

}

func getconsultsByOther(ctx *gin.Context) {
	condition := ctx.Query("condition")
	consults, err := findConsultsByOther(condition)
	if err != nil {
		ctx.JSON(http.StatusNotFound, "未找到！！")
	} else {
		ctx.JSON(http.StatusOK, consults)
	}
}

func deleteConsult(ctx *gin.Context) {
	conId := ctx.Query("id")
	id, _ := strconv.ParseUint(conId, 10, 64)
	err := adminDeleteConsult(uint(id))
	if err != nil {
		ctx.JSON(http.StatusBadRequest, gin.H{"error": err})
	} else {
		ctx.JSON(http.StatusOK, "删除成功！！")
	}
}
